//
//  CC_Recommend_CollectionViewCell.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CC_Recommend_CollectionViewCell.h"
#import "CCBaseCollectionView.h"
#import "CC_Custom_CollectionReusableView.h"  // 头视图
#import "CC_AutoScrollingAndCircleImage_CollectionViewCell.h"  // 第一区自定义cell

#import "CC_ImageViewAndTwoLabel_CollectionViewCell.h"  // 图片在上Label在下类型

#import "CC_Recommend_Model.h"

#import "CC_SpecialColumn_CollectionViewCell.h" // 精品听单cell类型
#import "CC_SpecialColumn_Model.h"  // 精品听单类型

#import "CC_Title_Model.h"

#define IMAGELABELCELL @"ImageViewAndTwoLabel_CollectionViewCell"
#define SPECIALCELL @"SpecialColumnCell"


@interface CC_Recommend_CollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, retain) CCBaseCollectionView *collectionView;


@property(nonatomic, retain) NSMutableArray *titleArray;  // 主题array;
@property(nonatomic, retain) NSArray *editorRecommendArray;  // 小编推荐数组
@property(nonatomic, retain) NSArray *specialColumnArray; // 精品听单数组


@property(nonatomic, retain) NSArray *titleMoreArray;  // 多数的titleArray;


@end


@implementation CC_Recommend_CollectionViewCell
- (void)dealloc
{
    [_titleMoreArray release];
    [_titleArray release];
    [_specialColumnArray release];
    [_editorRecommendArray release];
    [_collectionView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self handleData];
        
        [self handleDataOfHotRecommends];
        [self createCollectionView];
        
        
    }
    return self;
}

-(void)handleData{
    
    self.titleArray = [NSMutableArray array];
    
    self.editorRecommendArray = [NSArray array];
    self.specialColumnArray = [NSArray array];
    
    [CCNetTool GET:@"http://mobile.ximalaya.com/mobile/discovery/v4/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.98" body:nil headerFile:nil response:CCJSON success:^(id result) {
        

        
        NSDictionary *dic = result;
        
        NSDictionary *dicOfEditorRecommendAlbums = [dic objectForKey:@"editorRecommendAlbums"];
        
        // 添加titleModel;
        CC_Title_Model *titleModelOfEditor = [[CC_Title_Model alloc]initWithDic:dicOfEditorRecommendAlbums];
        [self.titleArray addObject:titleModelOfEditor];
        [titleModelOfEditor release];
        
        
        NSArray *listArray = [dicOfEditorRecommendAlbums objectForKey:@"list"];
        self.editorRecommendArray = [CC_Recommend_Model arrayFromArray:listArray];
        

        // 添加titlemodel
        NSDictionary *dicOfSpecialColumn = [dic objectForKey:@"specialColumn"];
        CC_Title_Model *titleModelOfSpecial = [[CC_Title_Model alloc]initWithDic:dicOfSpecialColumn];
        [self.titleArray addObject:titleModelOfSpecial];
        [titleModelOfSpecial release];
        
        NSArray *listArrayOfSpecialColumn = [dicOfSpecialColumn objectForKey:@"list"];
        
        self.specialColumnArray = [CC_SpecialColumn_Model arrayFromArray:listArrayOfSpecialColumn];
        
        NSLog(@"___%@", self.specialColumnArray);
        
        [self.collectionView reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)handleDataOfHotRecommends{
    
    self.titleMoreArray = [NSArray array];
    
   [CCNetTool GET:@"http://mobile.ximalaya.com/mobile/discovery/v2/recommend/hotAndGuess?code=43_210000_2102&device=iPhone&version=4.3.98" body:nil headerFile:nil response:CCJSON success:^(id result) {
       
       NSDictionary *dic = result;
       
       NSDictionary *dicOfHotRecommends = [dic objectForKey:@"hotRecommends"];
       
       NSArray *list = [dicOfHotRecommends objectForKey:@"list"];
       
       self.titleMoreArray = [CC_Title_Model arrayFromArray:list];
       
       [self.collectionView reloadData];
       
   } failure:^(NSError *error) {
    
   }];
    
    
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    
    flowLayout.sectionInset = UIEdgeInsetsMake(SPACED, SPACED, SPACED, SPACED);
    
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    
    self.collectionView = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 - 40 - 59) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];  // 颜色
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    // 注册头视图重用池
    [self.collectionView registerClass:[CC_Custom_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
    // 注册尾视图重用池
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    
    // 注册重用池
    
    [self.collectionView registerClass:[CC_AutoScrollingAndCircleImage_CollectionViewCell class] forCellWithReuseIdentifier:@"firstCell"];
    
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
  
    //ImageViewAndTwoLabel_CollectionViewCell
    [self.collectionView registerClass:[CC_ImageViewAndTwoLabel_CollectionViewCell class] forCellWithReuseIdentifier:IMAGELABELCELL];
    
    
    //SpecialColumnCell
     [self.collectionView registerClass:[CC_SpecialColumn_CollectionViewCell class] forCellWithReuseIdentifier:
      SPECIALCELL];
    
    
    
    [self.contentView addSubview:_collectionView];
  
    
    [flowLayout release];
    [_collectionView release];
    
}

// 区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
//    return 5;
    
    return self.titleMoreArray.count + 3;
    
}

// 每个区的item数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:      // 自动轮播图以及手动轮播
            return 1;
            break;
        case 1:  // 小编推荐
            return self.editorRecommendArray.count;
            break;
        case 2:
            return self.specialColumnArray.count;  // 精品听单
            break;
//        case 3:
//            return 1;
//            break;
        default:

        {
            CC_Title_Model *titleModel =[self.titleMoreArray objectAtIndex:(section - 3)];
            
            return titleModel.listArray.count;
            
        }
            break;
    }
    
    
    
    
}

// 内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0: // 自动轮播图 已经手动图
        {
            CC_AutoScrollingAndCircleImage_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
            return cell;
            
        }
            break;
        case 1:   // 小编推荐
        {
            CC_ImageViewAndTwoLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IMAGELABELCELL forIndexPath:indexPath];

            cell.recommendModel = [self.editorRecommendArray objectAtIndex:indexPath.row];
            
            return cell;
        }
             break;
        case 2:   // 精品听单
        {
        
            CC_SpecialColumn_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SPECIALCELL forIndexPath:indexPath];
            
            cell.specialModel = [self.specialColumnArray objectAtIndex:indexPath.row];
            
            return cell;
        }
        
        default:
        {
            CC_ImageViewAndTwoLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IMAGELABELCELL forIndexPath:indexPath];
            
            CC_Title_Model *titleModel =[self.titleMoreArray objectAtIndex:(indexPath.section - 3)];
            
            
            
            cell.recommendModel = [titleModel.listArray objectAtIndex:indexPath.row];
            
            
            return cell;

        }
            break;
    }
    
}


// 每个区itemsize的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:   // 自动轮播图 以及 手动轮播图
            return CGSizeMake(SCREENWIDTH, 270);
            break;
        case 1:   // 小编推荐类型
            return CGSizeMake((SCREENWIDTH - 6 * SPACED) / 3, (SCREENWIDTH - 6 * SPACED) / 3 * 1.7);
            
            break;
        case 2:   // 精品听单
            return CGSizeMake( SCREENWIDTH - 2 * SPACED  , (SCREENWIDTH - 6 * SPACED) / 3 );
            
            break;
        default:
             return  CGSizeMake((SCREENWIDTH - 6 * SPACED) / 3, (SCREENWIDTH - 6 * SPACED) / 3 * 1.7);
            break;
    }
    

    
}



// 返回头视图 和 尾部视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  
    if(kind == UICollectionElementKindSectionHeader){
   
        CC_Custom_CollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

//        if (indexPath.section >=1 && indexPath.section <= 2) {
//            headerView.titleModel = [self.titleArray objectAtIndex:indexPath.section];
//        }
        
        
        switch (indexPath.section) {
            case 0:
                
                break;
            case 1:
                
                break;
            case 2:
                
                break;
            default:
                headerView.titleModel = [self.titleMoreArray objectAtIndex:indexPath.section - 3];

                break;
        }
        
        
        
        
    headerView.backgroundColor = [UIColor whiteColor];  // 区头颜色
    
        return headerView;
     }else{
            
     UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
    
    footerView.backgroundColor = [UIColor grayColor];  // 区尾颜色
     footerView.alpha = 0.3;
         return footerView;
            
            
        }
    

}




// 可以设置每个区不同的最小距离
//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    
//    return 0;
//    
//}

// 区头的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return CGSizeMake(SCREENWIDTH, 0);
            break;
        case 1:
           return CGSizeMake(SCREENWIDTH, 45);
            break;
        case 2:
             return CGSizeMake(SCREENWIDTH, 45);
            break;
        case 3:
            return CGSizeMake(SCREENWIDTH, 45);
            break;
        default:
             return CGSizeMake(SCREENWIDTH, 45);
            break;
    }
    

}

// 区尾视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
 
    switch (section) {
        case 0:
            return CGSizeMake(SCREENWIDTH, 15);
            break;
        case 1:
              return CGSizeMake(SCREENWIDTH, 15);
            break;
        case 2:
              return CGSizeMake(SCREENWIDTH, 15);
            break;
        case 3:
              return CGSizeMake(SCREENWIDTH, 15);
            break;
        default:
              return CGSizeMake(SCREENWIDTH, 15);
            break;
    }

    
    
    return CGSizeMake(SCREENWIDTH, 15);
}





@end
